package cn.longge.service.impl;

import cn.longge.entity.SysRole;
import cn.longge.mapper.SysRoleMapper;
import cn.longge.service.IBaseService;
import cn.longge.service.ISysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements ISysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public PageInfo selectRoleAndOffice(int page, int pageSize, Map<String,Object> params){
        PageHelper.startPage(page,pageSize);
        List<SysRole> sysRoles = roleMapper.selectRoleAndOffice(params);
        return new PageInfo(sysRoles);
    }

    @Override
    public int deleteByUserIds(Long rid, List<Long> ids) {
        int i = roleMapper.deleteByUserIds(rid, ids);
        if(i < 1){
            throw new RuntimeException("删除失败");
        }
        return i;
    }

    @Override
    public int insertMany(Long rid, List<Long> addIds) {
        return roleMapper.insertMany(rid,addIds);
    }

    @Override
    public int updateByPrimaryKeySelective(SysRole role) {
        int i = super.updateByPrimaryKeySelective(role);
        if(null != role.getResources()){
            i += roleMapper.deleteFromSRR(role.getId());
            if(0 != role.getResources().size()){
                i += roleMapper.insertRoleResource(role.getId(),role.getResources());
            }
        }
        if(null != role.getOffices()){
            i += roleMapper.deleteFromSRO(role.getId());
            if(0 != role.getOffices().size()){
                i += roleMapper.insertRoleOffice(role.getId(),role.getOffices());
            }
        }
        return i;
    }
}
